/**
 * Created by Administrator on 2017/1/18.
 */
var screenHeight = window.screen.height;
var screenWidth = window.screen.width;

//详细地址中最多输入的数量
var MAX_ADDRESS_LENGTH = 30;
//名字最多输入的字符
var MAX_NAME_LENGTH = 8;

$(function () {
    initView();
    //开始提交
    $(".btn-submit").click(function () {
        //验证手机号码
        var reg = /^1[3|4|5|7|8][0-9]{9}$/;
        var phone = $(".receive li.phone input").val();   //得到的string类型的字符串
        if (reg.test(phone)){
            alert("手机号验证通过");
        }else {
            alert("手机号码输入有误");
        }
    });
});

function initView() {
    //设置body的高
    $("body").height(screenHeight);
    //按照比例设置div距离顶部的距离
    $(".receive").css({
        "top": screenWidth/36*20+"px"
    });
    //设置input的宽高
    var liWidth = $("li").width();
    var firstPL = parseInt($(".receive li.receive-person input").css("padding-left"));
    $(".receive li.receive-person input").width(liWidth - firstPL);
    var secondPL = parseInt($(".receive li.phone input").css("padding-left"));
    $(".receive li.phone input").width(liWidth - secondPL);
    var thirdPL = parseInt($(".receive-address input").css("padding-left"));
    $(".receive-address input").width(liWidth - thirdPL-10);

    //设置textarea的宽
    //先得到paddingLeft和paddingRight
    var PlAndPr = parseFloat($("textarea").css("padding-left"))+parseFloat($("textarea").css("padding-right"));
    $("textarea").width($(".receive").width()-PlAndPr);
    $(".div-submit").css("top",screenWidth/360*215+"px");
    

    //姓名和手机号码的限制
    limitInputNamePhone();

    //初始化地区选择器
    initSelectArea();
    //点击让详细地址里面的文字消失
    initArea();
}

function limitInputNamePhone() {
    var inputs = document.getElementById("receive").getElementsByTagName("input");
    inputs[0].oninput = inputs[0].onpropertychange = function () {
        var length = this.value.length;
        if (length>MAX_NAME_LENGTH){
            this.value = this.value.slice(0,MAX_NAME_LENGTH);
        }
    }

    var MAX_PHONE_LENGTH = 11;
    inputs[1].oninput = inputs[1].onpropertychange = function () {
        var length = this.value.length;
        if (length > MAX_PHONE_LENGTH){
            this.value = this.value.slice(0,MAX_PHONE_LENGTH);
        }
    }
}

function initArea() {
    var emptyTxt = "请填写详细地址,不少于5个字";

    $("textarea").hover(function () {
        if($(this).val() == emptyTxt){
            $(this).val("");
            $(this).css("color","#737373");
        }
    },function () {
        if($(this).val() == ""){
            $(this).val(emptyTxt);
            $(this).css("color","#ccc");
        }
    });
    $(".max-address").text("0/"+MAX_ADDRESS_LENGTH);
    var address = document.getElementById("user-address");
    address.oninput = function () {
        var length = this.value.length;
        if (length >= MAX_ADDRESS_LENGTH){
            this.value = this.value.slice(0,MAX_ADDRESS_LENGTH);
            length = MAX_ADDRESS_LENGTH;
        }
        $(".max-address").text(length+"/"+MAX_ADDRESS_LENGTH);
    }
}

function initSelectArea() {
    var area2 = new LArea();
    area2.init({
        'trigger': '#input-address-show',
        'valueTo': '#input-address-hidden',
        'keys': {
            id: 'value',
            name: 'text'
        },
        'type': 2,
        'data': [provs_data, citys_data, dists_data]
    });
}
























